{%extends 'base.html'%}
{%block title%}添加卡密{%endblock%}
{%block content%}
<div class="panel panel-info">
    <div class="list-group-item reed" style="background: #66bdff;">
        <h3 class="panel-title">
            <font color="#fff"><i class="fa fa-volume-up"></i>&nbsp;&nbsp;<b>添加卡密须知<用户必看></b></font>
          </h3>
    </div>
    <table class="table table-bordered">
        <tbody>
            <tr>
                <td align="center">
                    <font color="#808080">本站域名：{{domain}}</font>
                </td>
                <td align="center">
                    <font color="#808080">客服QQ:{{qq}}</font>
                </td>
            </tr>
        </tbody>
    </table>
</div>
<div class="list-group-item">
    <div id="myTabContent" class="tab-content">
        <div class="tab-pane fade in active" id="onlinebuy">
            <div class="form-group">
                <div class="input-group">
                    <div class="input-group-addon">商品分类</div>
                    <select name="tp_id" id="tp_tid" class="form-control" onchange="getPoint(this);">
                        {%for cate in Category.query.order_by(Category.cateid.asc()).all()%}
                        <option value="{{cate.cateid}}">{{cate.cate_name}}</option>
                        {%endfor%}
                    </select>
                </div>
            </div>
            <div class="form-group">
                <div class="input-group">
                    <div class="input-group-addon">选择商品</div>
                    <select name="gid" id="gid" class="form-control" onchange="getPrice(this)">
                    </select>
                </div>
            </div>
            <div class="form-group">
                <div class="panel panel-default">
                    <div class="panel-body" id="ginfo">
                        <div class="input-group-addon">卡密（一行一个）</div>
                        <textarea id="km" rows="10" cols="50" style="resize:none;"></textarea>
                    </div>
                </div>
            </div>
        </div>
    <input type="submit" id="submit_buy" class="btn btn-primary btn-block" value="添加卡密">
</div>
<script type="text/javascript">
function getPoint(e) {
    var d = $(e).val(); //获取该分类的ID
    var ii = layer.load(2, { shade: [0.1, '#fff'] });
    $.ajax({
        type: "POST",
        url: "{{url_for('faka.Selgo')}}",
        data: { "cateid": d },
        dataType: 'json',
        success: function(data) {
            layer.close(ii);

            //$("#gid").empty();
            //$("#gid").append(data.msg);
            $("#gid").html(data.msg);
            //$('#ginfo').html("");
            //$('#ginfo').hide();
        },
        error: function(data) {
            layer.close(ii);
            layer.msg('服务器错误');
        }
    });
}


//*******************提交按钮************************
$("#submit_buy").click(function() {
    var a = $('#kc').attr("alt");
    if (a <= 0) {
        layer.msg('该商品库存不足，无法购买！');
        return;
    }
    var gName = $("#gid").val(); //获取商品名称<展示>
    var gId = $("#gid option:selected").attr("id"); //获取商品ID *
    var km = $("#km").val().split('\n'); //获取商品价格<展示>

    var ii = layer.load(2, { shade: [0.1, '#fff'] });

    //验证卡密信息
    $.ajax({
        type: "POST",
        url: "{{url_for('faka.add_code')}}",
        data: { "goodid": gId,'km':JSON.stringify(km)},
        dataType: 'json',
        success: function(data) {
                layer.confirm(data.msg, {
                        btn: '确定' //按钮
                        , function(index) {
                        window.location.reload();
                        layer.close(index);
                        }
                    });
        },
        error: function(data) {
            layer.close(ii);
            layer.msg('服务器错误');
            return;
        }
    });
    layer.close(ii);

    var u = "type=" + type + "&name=" + gName + "&money=" + money + "&out_trade_no=" + out_trade_no + "&gid=" + gId;

    var str = "请核对订单信息！<br>订单编号：" + out_trade_no +
        "<br>商品名称：" + gName +
        "<br>商品价格：" + price +
        "<br>联系方式：" + num +
        "<br>付款方式：" + payfs;

    layer.confirm(str, {
        btn: ['立即付款', '取消付款'] //按钮
    }, function() {
        //异步创建订单
        var ii = layer.load(2, { shade: [0.1, '#fff'] })
        $.ajax({
            type: "POST",
            url: "{{url_for('faka.CreateOrder')}}",
            data: { "out_trade_no": out_trade_no, "gid": gId, "rel": num },
            dataType: 'json',
            //timeout : 5000,
            success: function(data) {
                layer.close(ii);
                if (data.code != 0) {
                    layer.msg('服务器错误');
                    return false;
                } else {
                    var qr = data.qr;
                    str = "微信扫码支付<br><img src='http://qr.liantu.com/api.php?&w=200&text=" + qr + "'>";
                    layer.confirm(str, {
                        btn: ['已付款', '取消付款'] //按钮
                    }, function(index) {
                        //do something
                        window.location.reload();
                        layer.close(index);
                    });
                }
            },
            error: function(data) {
                layer.close(ii);
                layer.msg('服务器错误');
                return false;
            }
        });
        //*********************************************


    }, function() {
        layer.msg('请认真选择您要购买的商品，本站已开启黑名单记录，不良操作会导致您无法继续使用本站', {
            time: 5000, //20s后自动关闭
            btn: ['明白了']
        });
    });
})
//生成单号
function d() {
    var d = new Date();
    var e;
    e = d.getFullYear() + "" + d.getMonth() + "" + d.getDate() + "" + d.getHours() + "" + d.getMinutes() + "" + d.getMilliseconds();
    return e;
}

//判断联系方式是否未数字
function checkLx(num) {
    var t = num; //这个就是我们要判断的值了
    if (!isNaN(t) && t != "") {
        return true;
    } else {
        return false;
    }
}
</script>
<script type="text/javascript">
$(function() {
    //获取商品
    getPoint($("#tp_tid"));
})
</script>
{%endblock content%}
